#include "tests.h"

void testBinarySensorValueGeneration() {
	int i = 0;
	const int cycles = 100;
	int trueCount = 0;
	int falseCount = 0;
	int value = 0;

	puts("Binary sensor test");

	for (i = 0; i < cycles; i++) {
		value = generateBinaryValue();
		if (value) {
			trueCount++;
		} else {
			falseCount++;
		}
		//printf("%d\n", value);
	}
	printf("Total:       %d\n", cycles);
	printf("Chance: 1 in %d\n", BINARY_SENSOR_CHANCE_FOR_TRUE);
	printf("True count:  %d\n", trueCount);
	printf("False count: %d\n", falseCount);
}

void testAnalogSensorValueGeneration() {
	int i;

	puts("Analog sensor test");

	for (i = 0; i < 50; i++) {
		//printf("%f\n", generateAnalogValue());
	}
}

void testJsonConversion() {
	char json[1000];
	struct AnalogSensor analogSensor;
	struct BinarySensor binarySensor;

	struct AnalogSensor aSensors[2];
	struct BinarySensor bSensors[2];

	strcpy(analogSensor.lowerLimitAlarmName, "LOWER ALARM!");
	strcpy(analogSensor.upperLimitAlarmName, "UPPER ALARM!");
	strcpy(analogSensor.unit, "Pascal");
	strcpy(analogSensor.name, "Pietje");
	strcpy(analogSensor.timeStamp, "30-12-9999");
	analogSensor.value = 42.42;
	analogSensor.lowerLimit = 40.0;
	analogSensor.upperLimit = 45.0;

	strcpy(binarySensor.alarmName, "ALARM!");
	strcpy(binarySensor.name, "Pietje");
	strcpy(binarySensor.timeStamp, "30-12-9999");
	binarySensor.value = 0;

	aSensors[0] = analogSensor;
	aSensors[1] = analogSensor;
	bSensors[0] = binarySensor;
	bSensors[1] = binarySensor;

	puts("Complete JSON object\n");
	sensorsToJson(aSensors, 2, aSensors, 2, bSensors, 2, json);
	puts(json);

	puts("Sensor alarms");
	createAlarmJson(analogSensor.name, analogSensor.upperLimitAlarmName, json);
	puts(json);
}

int getTestJsonObject(char json[]) {
	struct AnalogSensor analogSensor;
	struct BinarySensor binarySensor;
	struct AnalogSensor aSensors[2];
	struct BinarySensor bSensors[2];

	strcpy(analogSensor.lowerLimitAlarmName, "LOWER ALARM!");
	strcpy(analogSensor.upperLimitAlarmName, "UPPER ALARM!");
	strcpy(analogSensor.unit, "Pascal");
	strcpy(analogSensor.name, "Pietje");
	strcpy(analogSensor.timeStamp, "30-12-9999");
	analogSensor.value = 42.42;
	analogSensor.lowerLimit = 40.0;
	analogSensor.upperLimit = 45.0;

	strcpy(binarySensor.alarmName, "ALARM!");
	strcpy(binarySensor.name, "Pietje");
	strcpy(binarySensor.timeStamp, "30-12-9999");
	binarySensor.value = 0;

	aSensors[0] = analogSensor;
	aSensors[1] = analogSensor;
	bSensors[0] = binarySensor;
	bSensors[1] = binarySensor;

	return sensorsToJson(aSensors, 2, aSensors, 2, bSensors, 2, json);
}

void testCommunication() {
	initSocket();
}

void testJsonParsing() {
	initSensors();
	char jsonTest[400];
	strcpy(jsonTest, "{\"analogAlarmChange\":{\"sensorname\":\"AnalogSensor1\",\"lowerboundary\":20,\"upperboundary\":55}}");
	struct AnalogAlarmChangeStruct alarmStructA;
	struct BinaryAlarmChangeStruct alarmStructB;
	alarmStructA.lowerLimit = 0;
	strcpy(alarmStructA.lowerLimitAlarmName, "loweralarm!!!11!1!");
	strcpy(alarmStructA.name, "sensor42");
	alarmStructA.upperLimit = 123123;
	strcpy(alarmStructA.upperLimitAlarmName, "upperalarm!!!11!1!111!!!!!");

	int parseReturn = parseAlarmChangeJson(jsonTest, &alarmStructA, &alarmStructB);
	if (parseReturn == 0) {
		// Parsing was unsuccessful, ignore
	} else if (parseReturn == 1) {
		// Parsed analog change
		findAndChangeAnalogAlarm(&alarmStructA);
	} else if (parseReturn == 2) {
		// Parsed binary change
		findAndChangeBinaryAlarm(&alarmStructB);
	}
}
